#read loan csv fileaddiction_data <-read.csv("data/cleaned_addiction.csv")#addiction_tibble <- tibble(addiction_data) #use tibble to make addiction_data a dataframe#remove NA rows in the Count_drug_overdose_deaths field addiction_dropna <-na.omit(addiction_data)#load full datasetfull_data <-read_csv("data/joined_data.csv")#load population data pop_data <-read_csv("data/population.csv")#load the usa map shape fileusa_sf <-read_sf("data/tl_2024_us_state/tl_2024_us_state.shp")
Code
#subset population data to merge with addiction_deathdate_df2 dataframe pop_subset <- pop_data %>%select(c("fips_code", "state", "area_name", "census_2020_pop"))#remove row with fips_code = 0 pop_subset <- pop_subset[pop_subset$state !="US", ]#rename fips_code column name pop_subset <- pop_subset %>%rename("FIPS"="fips_code")#merge the full_data2 and addiction_deathdate_df2 dataframes addiction_pop_join <- addiction_dropna %>%left_join(pop_subset, by ="FIPS")
Code
#create column of overdose deaths total per stateaddiction_pop_join2 <- addiction_pop_join %>%group_by(STATE_NAME) %>%mutate(State_deaths_total =sum(Count_drug_overdose_deaths, na.rm =TRUE))#create column of overdose deaths per capita state level addiction_pop_join2 <- addiction_pop_join2 %>%mutate(State_deaths_per_capita = (State_deaths_total/census_2020_pop))#create column of log transformed overdose deaths per capita at state level addiction_pop_join2 <- addiction_pop_join2 %>%mutate(Log_state_deaths_per_capita =ifelse(State_deaths_total >0, log(State_deaths_total), NA))#create column for text tooltip addiction_pop_join2 <- addiction_pop_join2 %>%mutate(log_state_hover_text =paste0( "State: ", STATE_NAME, "<br>","State FIP: ", STATEFIPS, "<br>","Average Deaths per Capita: ", State_deaths_total))
Code
#create column for total overdose deaths by countyaddiction_pop_join2 <- addiction_pop_join2 %>%group_by(COUNTYNAME) %>%mutate(Total_overdosedeaths_county =sum(Count_drug_overdose_deaths, na.rm =TRUE))#create column of overdose deaths per capita count level addiction_pop_join2 <- addiction_pop_join2 %>%mutate(County_deaths_per_capita = (Total_overdosedeaths_county/census_2020_pop))#create column of log transformed overdose deaths per capita at county level addiction_pop_join2 <- addiction_pop_join2 %>%mutate(Log_county_deaths_per_capita =ifelse(County_deaths_per_capita >0, log(County_deaths_per_capita), NA))#rename state_name column to name in addiction_pop_join2 addiction_pop_join2 <- addiction_pop_join2 %>%rename("NAME"="STATE_NAME")#join addiction_pop_join2 woth usa_sf dataset add_pop_sf_join <- usa_sf %>%left_join(addiction_pop_join2, by="NAME")
Code
#create choropleth map with log transformed overdose deaths per capita state level#source code site: https://plotly.com/r/choropleth-maps/g2 <-list( scope ='usa',projection =list(type ='albers usa'),showlakes =TRUE,lakecolor =toRGB('white'))fig2 <-plot_ly(type="choropleth",locations=addiction_pop_join2$ST_ABBREV,z=addiction_pop_join2$Log_state_deaths_per_capita,text=addiction_pop_join2$log_state_hover_text,hoverinfo ="text",locationmode ="USA-states",colorscale="Viridis",colorbar =list(title ="Log Drug Overdose Deaths"),marker =list(line =list(width =0)))fig2 <- fig2 %>%layout(title ="Log Transformed Drug Overdose Deaths \n Per Capita in the US from 2020 to 2024 \n State Level",geo = g2)fig2